/**
 * @param {number[]} tickets
 * @param {number} k
 * @return {number}
 */
 var timeRequiredToBuy = function(tickets, k) {
  let ans = 0
  let cost = tickets[k]
  for(let i = 0; i<tickets.length; i++){
    ans += Math.min(tickets[i], cost)
  }
  for(let i = k+1; i<tickets.length; i++){
    if(tickets[i] >= cost){
      ans--
    }
  }
  return ans
};